package com.beiding.jsonanalyzer.analyze.func.openfunc;

import com.beiding.jsonanalyzer.analyze.func.OneFunc;
import com.beiding.jsonanalyzer.analyze.func.base.MultiValueFunc;
import com.beiding.jsonanalyzer.analyze.func.base.OneFuncAdaptor;

/**
 * 字符串拼接
 *
 * @author 丁常磊
 * @date 2022/5/19 11:23
 */

public class Concat {


    public static OneFunc of(Object... keys) {
        return FuncUtils.buildForMultiValue(new OneFuncAdaptor<MultiValueFunc>() {
            @Override
            protected MultiValueFunc newInstance() {
                return new MultiValueFunc() {
                    @Override
                    protected Object forMultiValues(Object[] values) {
                        StringBuilder builder = new StringBuilder();
                        for (Object value : values) {
                            builder.append(value);
                        }
                        return builder.toString();
                    }

                    @Override
                    public String toString() {
                        if (gets.length == 0) {
                            return "concat()";
                        }
                        StringBuilder builder = new StringBuilder();
                        builder.append("concat(");
                        builder.append(gets[0]);
                        for (int i = 1; i < gets.length; i++) {
                            builder.append(",").append(gets[i]);
                        }
                        builder.append(")");
                        return builder.toString();
                    }
                };
            }
        }, keys);
    }


}
